package com.tyd.cams;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;

public class GuokaoChenjiActivity extends AppCompatActivity {
    private WebView webView;
    public WebSettings webSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guokao_chenji);
        String url = getIntent().getStringExtra("url");
        webView = findViewById(R.id.gk_webview);
//        webView.setWebViewClient(new WebViewClient());

        webView.setWebViewClient(new WebViewClient() {
            //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url);
                return true;
            }
        });
        webView.loadUrl(url);
        webSettings = webView.getSettings();
        /*String set="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36";
        webSettings.setUserAgentString(set);*/
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);//自适应屏幕
        webSettings.setLoadWithOverviewMode(true);//自适应屏幕
        webSettings.setBuiltInZoomControls(true);//自动缩放
        webSettings.setSupportZoom(true);//自动缩放

        // 允许调用 JS，因为网页地图使用的是 JS 定位
        webSettings.setJavaScriptEnabled(true);
        //启用数据库
        webSettings.setDatabaseEnabled(true);
        //启用地理定位，默认为true
        webSettings.setGeolocationEnabled(true);
        //设置定位的数据库路径
        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        webSettings.setGeolocationDatabasePath(dir);
        //开启DomStorage缓存
        webSettings.setDomStorageEnabled(true);
//        WebView监听网页内部返回键 实现前进、后退、与刷新功能
        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    //按返回键操作并且能回退网页
                    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                        //后退
                        webView.goBack();
                        return true;
                    }
                }
                return false;
            }
        });

      /*  webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }

        });*/
//        Map<String, String> header = new HashMap<>();
//        header.put("Referer", "https://m7m3mw.smartapps.cn/pages/fuwushouye/index?service_id=003");
        webView.loadUrl(url);


    }
}